package com.huang.wangpan.strategy.process;

import com.alibaba.fastjson.JSONObject;
import com.huang.wangpan.service.GetDataService;
import com.huang.wangpan.utils.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: liminghui
 * @Date: 2020/11/7 12:37
 * @Version:
 * @Description:
 */
@Component
public class RenameProcess implements CommonProcess {

    @Autowired
    GetDataService getDataService;

    private static final Logger logger = LoggerFactory.getLogger(RenameProcess.class);

    @Override
    public void process(String method, HttpServletRequest request, HttpServletResponse response) {
        String currentpath = request.getParameter("currentpath");
        ResultData rd = new ResultData();
        String newname = request.getParameter("newname");
        String filename = request.getParameter("filename");
        logger.info("RenameProcess-->process; rename的新文件名newname={}", newname);

        if (!getDataService.isSameName(request,currentpath, newname)) {
            getDataService.rename(request,currentpath, newname, filename);
        } else {
            logger.warn("RenameProcess-->process; rename的文件名{}相同！！！", newname);
            JSONObject flag = new JSONObject();
            flag.put("flag", false);
            rd.setData(flag);
            rd.writeToResponse(response);
        }
    }
}
